{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0287410b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "playsound is relying on another python subprocess. Please use `pip install pygobject` if you want playsound to run more efficiently.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "You are not visible at all\n",
      "current progress is :  1\n",
      "current progress is :  2\n",
      "current progress is :  3\n",
      "current progress is :  4\n",
      "current progress is :  5\n",
      "current progress is :  6\n",
      "current progress is :  7\n",
      "current progress is :  8\n",
      "current progress is :  9\n",
      "current progress is :  10\n",
      "current progress is :  11\n",
      "current progress is :  12\n",
      "current progress is :  13\n",
      "current progress is :  14\n",
      "current progress is :  15\n",
      "current progress is :  16\n",
      "current progress is :  17\n",
      "current progress is :  18\n",
      "current progress is :  19\n",
      "current progress is :  20\n",
      "current progress is :  21\n",
      "current progress is :  22\n",
      "current progress is :  23\n",
      "current progress is :  24\n",
      "current progress is :  25\n",
      "current progress is :  26\n",
      "current progress is :  27\n",
      "current progress is :  28\n",
      "current progress is :  29\n",
      "current progress is :  30\n",
      "current progress is :  31\n",
      "current progress is :  32\n",
      "current progress is :  33\n",
      "current progress is :  34\n",
      "current progress is :  35\n",
      "current progress is :  36\n",
      "current progress is :  37\n",
      "current progress is :  38\n",
      "current progress is :  39\n",
      "current progress is :  40\n",
      "current progress is :  41\n",
      "current progress is :  42\n",
      "current progress is :  43\n",
      "current progress is :  44\n",
      "current progress is :  45\n",
      "current progress is :  46\n",
      "current progress is :  47\n",
      "current progress is :  48\n",
      "current progress is :  49\n",
      "current progress is :  50\n",
      "current progress is :  51\n",
      "current progress is :  52\n",
      "current progress is :  53\n",
      "current progress is :  54\n",
      "current progress is :  55\n",
      "current progress is :  56\n",
      "current progress is :  57\n",
      "current progress is :  58\n",
      "current progress is :  59\n",
      "current progress is :  60\n",
      "current progress is :  61\n",
      "current progress is :  62\n",
      "current progress is :  63\n",
      "current progress is :  64\n",
      "current progress is :  65\n",
      "current progress is :  66\n",
      "current progress is :  67\n",
      "current progress is :  68\n",
      "current progress is :  69\n",
      "current progress is :  70\n",
      "current progress is :  71\n",
      "current progress is :  72\n",
      "DEAD  257.53249168395996\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import mediapipe as mp \n",
    "import cv2\n",
    "import numpy as np\n",
    "import time\n",
    "from playsound import playsound\n",
    "\n",
    "cap = cv2.VideoCapture(0)\n",
    "cPos = 0\n",
    "startT = 0  \n",
    "endT = 0\n",
    "userSum = 0\n",
    "dur = 0\n",
    "isAlive = 1\n",
    "isInit = False\n",
    "cStart, cEnd = 0,0\n",
    "isCinit = False\n",
    "tempSum = 0\n",
    "winner = 0\n",
    "inFrame = 0\n",
    "inFramecheck = False\n",
    "thresh = 180\n",
    "\n",
    "def calc_sum(landmarkList):\n",
    "\t\n",
    "\ttsum = 0\n",
    "\tfor i in range(11, 33):\n",
    "\t\ttsum += (landmarkList[i].x * 480)\n",
    "\n",
    "\treturn tsum\n",
    "\n",
    "def calc_dist(landmarkList):\n",
    "\treturn (landmarkList[28].y*640 - landmarkList[24].y*640)\n",
    "\n",
    "def isVisible(landmarkList):\n",
    "\tif (landmarkList[28].visibility > 0.7) and (landmarkList[24].visibility > 0.7):\n",
    "\t\treturn True\n",
    "\treturn False\n",
    "\n",
    "mp_pose = mp.solutions.pose\n",
    "pose = mp_pose.Pose()\n",
    "drawing = mp.solutions.drawing_utils\n",
    "\n",
    "im1 = cv2.imread('im1.png')\n",
    "im2 = cv2.imread('im2.png')\n",
    "\n",
    "currWindow = im1\n",
    "\n",
    "while True:\n",
    "\n",
    "\t_, frm = cap.read()\n",
    "\trgb = cv2.cvtColor(frm, cv2.COLOR_BGR2RGB)\n",
    "\tres = pose.process(rgb)\n",
    "\tfrm = cv2.blur(frm, (5,5))\n",
    "\tdrawing.draw_landmarks(frm, res.pose_landmarks, mp_pose.POSE_CONNECTIONS)\n",
    "\n",
    "\tif not(inFramecheck):\n",
    "\t\ttry:\n",
    "\t\t\tif isVisible(res.pose_landmarks.landmark):\n",
    "\t\t\t\tinFrame = 1 \n",
    "\t\t\t\tinFramecheck = True\n",
    "\t\t\telse: \n",
    "\t\t\t\tinFrame = 0\n",
    "\t\texcept:\n",
    "\t\t\tprint(\"You are not visible at all\")\n",
    "\n",
    "\tif inFrame == 1:\n",
    "\t\tif not(isInit):\n",
    "\t\t\tplaysound('greenLight.mp3')\n",
    "\t\t\tcurrWindow = im1\n",
    "\t\t\tstartT = time.time()\n",
    "\t\t\tendT = startT\n",
    "\t\t\tdur = np.random.randint(1, 5)\n",
    "\t\t\tisInit = True\n",
    "\t\t\n",
    "\t\tif (endT - startT) <= dur:\n",
    "\t\t\ttry:\n",
    "\t\t\t\tm = calc_dist(res.pose_landmarks.landmark)\n",
    "\t\t\t\tif m < thresh:\n",
    "\t\t\t\t\tcPos += 1\n",
    "\n",
    "\t\t\t\tprint(\"current progress is : \", cPos)\n",
    "\t\t\texcept:\n",
    "\t\t\t\tprint(\"Not visible\")\n",
    "\n",
    "\t\t\tendT = time.time()\n",
    "\n",
    "\t\telse:\t\t\n",
    "\n",
    "\t\t\tif cPos >= 100:\n",
    "\t\t\t\tprint(\"WINNER\")\n",
    "\t\t\t\twinner = 1\n",
    "\n",
    "\t\t\telse:\n",
    "\t\t\t\tif not(isCinit):\n",
    "\t\t\t\t\tisCinit = True \n",
    "\t\t\t\t\tcStart = time.time()\n",
    "\t\t\t\t\tcEnd = cStart\n",
    "\t\t\t\t\tcurrWindow = im2\n",
    "\t\t\t\t\tplaysound('redLight.mp3')\n",
    "\t\t\t\t\tuserSum = calc_sum(res.pose_landmarks.landmark)\n",
    "\n",
    "\t\t\t\tif (cEnd - cStart) <= 3:\n",
    "\t\t\t\t\ttempSum = calc_sum(res.pose_landmarks.landmark)\n",
    "\t\t\t\t\tcEnd = time.time()\n",
    "\t\t\t\t\tif abs(tempSum - userSum) > 150:\n",
    "\t\t\t\t\t\tprint(\"DEAD \", abs(tempSum - userSum))\n",
    "\t\t\t\t\t\tisAlive = 0\n",
    "\n",
    "\t\t\t\telse:\n",
    "\t\t\t\t\tisInit = False\n",
    "\t\t\t\t\tisCinit = False\n",
    "\n",
    "\n",
    "\t\tcv2.circle(currWindow, ((55 + 6*cPos),280), 15, (0,0,255), -1)\n",
    "\n",
    "\t\tmainWin = np.concatenate((cv2.resize(frm, (800,400)), currWindow), axis=0)\n",
    "\t\tcv2.imshow(\"Main Window\", mainWin)\n",
    "\t\t#cv2.imshow(\"window\", frm)\n",
    "\t\t#cv2.imshow(\"light\", currWindow)\n",
    "\n",
    "\telse:\n",
    "\t\tcv2.putText(frm, \"Please Make sure you are fully in frame\", (20,200), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,255,0), 4)\n",
    "\t\tcv2.imshow(\"window\", frm)\n",
    "\n",
    "\n",
    "\tif cv2.waitKey(1) == 27 or isAlive == 0 or winner == 1:\n",
    "\t\tcv2.destroyAllWindows()\n",
    "\t\tcap.release()\n",
    "\t\tbreak\n",
    "\n",
    "\n",
    "frm = cv2.blur(frm, (5,5))\n",
    "\n",
    "if isAlive == 0:\n",
    "\tcv2.putText(frm, \"You are Dead\", (50,200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 4)\n",
    "\tcv2.imshow(\"Main Window\", frm)\n",
    "\n",
    "if winner == 1:\n",
    "\tcv2.putText(frm, \"You are Winner\", (50,200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 4)\n",
    "\tcv2.imshow(\"Main Window\", frm)\n",
    "\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c876976",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
